package com.joke.auto;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.jdbc.mysql.MysqlLocalConnect;
import com.jdbc.mysql.MysqlServerConnect;
import com.joke.util.Const;
import com.joke.util.DateUtil;
import com.joke.util.DownloadUtil;
import com.joke.util.HttpClientGet;
import com.joke.util.StringUtil;

public class FetchImage {
	private static final Logger logger = LoggerFactory.getLogger(FetchImage.class);

//	private static String neihahImage = "http://www.neihanshequ.com/pic/";
	private static String pengfuGifImage = "https://www.pengfu.com/qutu_(*).html";
//	private static String pengfuJpgImage = "https://www.pengfu.com/qutu_(*).html";
	
//	private static String nhCookie = "tt_webid=6530444013979846151; uuid=\"w:339f486c971d4499a6ca6e531589d304\"; _ga=GA1.2.2083410321.1520487485; csrftoken=dacecabf09780373b7ce80e7b9efac97; _gid=GA1.2.40257319.1521793305";
	private static String pfCookie = "Hm_lvt_53756750bad0066507bd205b907f59a3=1520563525,1520594801,1520909758,1520994206";


	private Date day = new Date();
	private String date = DateUtil.formatString(day, "yyyyMM");
	private String Y = DateUtil.formatString(day, "yyyy");
	private String M = DateUtil.formatString(day, "MM");
	private String D = DateUtil.formatString(day, "dd");
	private String ymd = Y+"/"+M+"/"+D;
    String writeImagePath=Const.fabu+date+Const.localImage;
	
	public static void main(String[] args) {
		new FetchImage().fetch();
	}

	public void fetch() {
		Map<String, String> imageMap = new HashMap<>();
//		neihanImage(imageMap);
		pengfuGifImage(imageMap);
		logger.info("抓取的<趣图>条数："+imageMap.size());
		//保存数据库  本地和服务器
    	MysqlServerConnect.insertImage(imageMap);
    	//保存入库趣图
    	DownloadUtil.writeImage(writeImagePath, imageMap);
/*		System.out.println(imageMap.size());
		for (String key : imageMap.keySet()) {
			System.out.println(key);
			System.out.println(imageMap.get(key));
		}*/
	}
	
/*	private Map<String, String> neihanImage(Map<String, String> imageMap) {
		for (int i = 1; i < 2; i++) {
			String contentHTML = getContent(neihahImage,nhCookie,i);
			parseNHImageContent(contentHTML,imageMap);
		}
		return imageMap;
	}*/
	
	private Map<String, String> pengfuGifImage(Map<String, String> imageMap) {
		for (int i = 1; i < 3; i++) {
			String contentHTML = getContent(pengfuGifImage,pfCookie,i);
			parsePFGifImageContent(contentHTML,imageMap);
		}
		return imageMap;
	}

	private String getContent(String url,String cookie, int i) {
		HttpClientGet.createDefaultHttpClient();
		Map<String, String> header = new HashMap<>();
		header.put("Cookie",cookie);
		HttpClientGet.setHeader(header);
		if(i==0) return null;
		else if(i>0) url = url.replace("(*)", String.valueOf(i));
		String contentHTML = HttpClientGet.sendHttpGet(url);
		return contentHTML;
	}

	@SuppressWarnings("unused")
	private Map<String, String> parseNHImageContent(String contentHTML,Map<String, String> imageMap) {
		if(contentHTML==null||"".equals(contentHTML)) return imageMap;
		Document doc = Jsoup.parse(contentHTML);  
		Elements links = doc.select("[id=groupImage]");  
		String content = null;
		String imagePath = null;
		String imageInfo = null;
		JSONObject json = null;
		String gif = null;
    	MysqlLocalConnect.getConnect();
		for (Element element : links) {
			content = element.attr("alt").trim();
			imagePath = element.attr("data-src").trim();
			imageInfo = element.attr("data-image-info").trim();
			json = JSONObject.parseObject(imageInfo);
			gif = json.getString("is_gif");
			String imageName = "";
			if("1".equals(gif)&&StringUtil.isNotEmpty(imagePath)) {
				if(!imagePath.endsWith(".gif")) {
					imagePath += ".gif";
				}
				imageName = imagePath.substring(imagePath.lastIndexOf("/")+1, imagePath.length());
				//下载文件  并保存到本地，下载的时候去重这样保证下载的都是新数据！后面上传到服务器的时候不用再去重
				if (StringUtil.isNotEmpty(imageName)&&StringUtil.check(content)&&MysqlLocalConnect.query(content, imageName)) {
					boolean a = DownloadUtil.downloadImg(imagePath,Const.picPath+ymd+Const.o+imageName);
					if(a) imageMap.put(StringUtil.reSpecial(content),imageName);
				}	
			}
		}
		MysqlLocalConnect.close();
		return imageMap;
	}
	
	private Map<String, String> parsePFGifImageContent(String contentHTML,Map<String, String> imageMap) {
		if(contentHTML==null||"".equals(contentHTML)) return imageMap;
		Document doc = Jsoup.parse(contentHTML);  
		Elements links = doc.select("[class=list-item bg1 b1 boxshadow]");  
		String linkOuterH = null;  
		Document doc1 = null;  
		Elements links1 = null; 
		String content = null;
		Elements imageEle = null; 
		String imagePath = null;
    	MysqlLocalConnect.getConnect();
		for (Element element : links) {
			linkOuterH = element.outerHtml();  
			doc1 = Jsoup.parse(linkOuterH);  
			links1 = doc1.select("[class=dp-b]");  
			content = links1.text().trim();
			imageEle = doc1.select("[gifsrc$=.gif]");  
			imagePath = imageEle.attr("gifsrc").trim();
			String imageName = "";
			if(StringUtil.isNotEmpty(imagePath)) {
				imageName = imagePath.substring(imagePath.lastIndexOf("/")+1, imagePath.length());
				//下载文件  并保存到本地，下载的时候去重这样保证下载的都是新数据！后面上传到服务器的时候不用再去重
				if (StringUtil.isNotEmpty(imageName)&&StringUtil.check(content)
						&&MysqlLocalConnect.query(content, imageName)) {
					boolean a = true;
					a = DownloadUtil.downloadImg(imagePath,Const.picPath+ymd+Const.o+imageName);
					if(a) imageMap.put(StringUtil.reSpecial(content), imageName);
				}			
			}
			
		}
		MysqlLocalConnect.close();
		return imageMap;
	}
	
}
